/*
 * Copyright 2021-2030 itbox.cn All right reserved. This software is the confidential and proprietary information
 * of itbox.cn ("Confidential Information"). You shall not disclose such Confidential Information and shall use it
 * only in accordance with the terms of the license agreement you entered into with itbox.cn.
 */
package cn.itbox.example.test.controller;

import cn.itbox.uap.redis.util.ItboxRedisStringService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @BelongProject springboot-study
 * @BelongPackage cn.itbox.hyb.example.test.controller
 * @Description TODO
 * @ClassName RedisController
 * @Author xingc
 * @Date 2024/1/9 18:51
 * @Version 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/redis")
public class RedisController {
    @Autowired
    private ItboxRedisStringService<String> redisStringService;

    @GetMapping(value = "/testString")
    public String testRedisString() {
        try {
            // redis key
            String key = "itbox-string";
            // 1、赋值且100秒过期
            redisStringService.set(key,"123", 100, TimeUnit.SECONDS);
            // 2、取值
            String value = redisStringService.get(key);
            log.info("String value：{}", value);
            // 3、key是否存在
            Boolean hasKey = redisStringService.hasKey(key);
            log.info("String hasKey：{}", hasKey);
            // redis key
            String incr_key_long = "itbox-string-incr-long";
            // 4、从0开始，递增2且100秒过期
            Long increment = redisStringService.increment(incr_key_long, 2, 100, TimeUnit.SECONDS);
            log.info("String increment：{}", increment);
            // 5、从0开始，递增1.1且200秒过期
            String incr_key_double = "itbox-string-incr-double";
            Double incrementDouble = redisStringService.increment(incr_key_double, 1.1D, 200, TimeUnit.SECONDS);
            log.info("String incrementDouble：{}", incrementDouble);
            // 6、递减1且100秒过期
            Long decrement = redisStringService.decrement(incr_key_long, 1, 100, TimeUnit.SECONDS);
            log.info("String decrement：{}", decrement);
            return value;
        } catch (Exception e) {
            log.error("testRedisString exception. e:{}", e);
        }
        return "error";
    }
}
